﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Noebe.Orm.Core;
using System.Data;
using Pixysoft.Framework.Noebe.Orm.Controller;
using Pixysoft.TestDrivens;
using System.Collections;

namespace Pixysoft.Framework.Noebe.Orm.testdriven
{
    class tc_002
    {
        public void ObjectTo()
        {
            Person person = Create<Person>();

            Assert.Write(ConversionController.ObjectToPrimaryKey(person));

            ObjectProxy proxy = ConversionController.ObjectToProxy(person);

            person = ConversionController.ProxyToObject(typeof(Person), proxy) as Person;
        }

        public void ListTo()
        {
            Person person = Create<Person>();

            List<Person> list = new List<Person>();
            list.Add(person);

            foreach (Int64 sid in ConversionController.ObjectListToPrimaryKeyList(list))
                Console.WriteLine(sid);

            List<ObjectProxy> proxyList = ConversionController.ObjectListToProxyList(list);

            IList rlist = ConversionController.ProxyListToObjectList(typeof(List<Person>), proxyList);

            ConversionController.ProxyListToCollection(typeof(Person[]), proxyList);
        }

        public void ReflectionTo()
        {
            object value = 12;

            ConversionController.ObjectToValue(typeof(int), value);

            string value2 = Sex.Lady.ToString();

            ConversionController.StringToEnum(typeof(Sex));

            ConversionController.StringToEnum(typeof(Sex), value2);

            List<int> ilist = new List<int>();
            ilist.Add(12);
            ConversionController.IListToValueList(typeof(List<int>), ilist);


            int[] iarray = new int[] { 1, 2, 3 };
            ConversionController.IListToValueCollection(typeof(int[]), iarray);

        }

        private T Create<T>()
        {
            Type type = typeof(T);

            string objectName = ReflectHelper.GetInterfaceTypeName(type);

            string tableName = MappingHelper.GetTableName(objectName);

            string sidName = MappingHelper.GetSysIDName();

            DataTable table = new DataTable();
            table.Columns.Add("STRINGVALUE");
            table.Columns.Add("INTVALUE");
            table.Columns.Add("ENUMVALUE");
            table.Columns.Add("SID");
            table.Columns.Add("CREATEDATE");
            table.Columns.Add("MODIDATE");
            table.Columns.Add("GROUPCODE");

            DataRow row = table.NewRow();

            row[sidName] = StringHelper.CreatePrimaryKey();

            ObjectProxy proxy = new ObjectProxy(StructStatus.Insert, row);

            return new DynamicOrmProxy<T>(proxy).GetTransparentProxy();
        }
    }
}
